package com.webvihar.access2db.activities;

import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.webvihar.access2db.R;
import com.webvihar.access2db.model.Session;
import com.webvihar.common.db.DbService;
import com.webvihar.common.utils.Validation;

public class AddSessionActivity extends BaseActivity implements OnClickListener {

	private DbService db;
	private TextView titleTxt;
	private ImageButton titleActionButton;
	private EditText nameTxt;
	private EditText hostTxt;
	private EditText portTxt;
	private EditText usernameTxt;
	private EditText passwordTxt;
	private Button addSessionBtn;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
		setContentView(R.layout.add_session);
		setTitleBar();
		initUI();
		setListeners();
		initDb();
	}

	public void setTitleBar() {
		titleTxt = (TextView) findViewById(R.id.title_txt);
		titleTxt.setText(getString(R.string.sessions_txt));
		titleActionButton = (ImageButton) findViewById(R.id.title_action_image_btn);
		titleActionButton.setVisibility(View.GONE);
	}

	private void initUI(){
		nameTxt = (EditText)findViewById(R.id.add_session_name_txt);
		hostTxt = (EditText)findViewById(R.id.add_session_host_txt);
		portTxt = (EditText)findViewById(R.id.add_session_port_txt);
		usernameTxt = (EditText)findViewById(R.id.add_session_username_txt);
		passwordTxt = (EditText)findViewById(R.id.add_session_password_txt);
		addSessionBtn = (Button)findViewById(R.id.add_session_add_edit_delete_btn);
		addSessionBtn.setText(getResources().getString(R.string.add_session_txt));
	}

	private void setListeners() {
		addSessionBtn.setOnClickListener(this);
	}

	private void initDb() {
		db = new DbService(this);
	}

	@Override
	public void onClick(View v) {
		processData();
	}

	private void processData() {
		String name = nameTxt.getText().toString();
		String host = hostTxt.getText().toString();
		String port = portTxt.getText().toString();
		String username = usernameTxt.getText().toString();
		String password = passwordTxt.getText().toString();

		if(isSessionNameValid(name)) {
			if(isDataValid(name,host,port,username,password)) {
				saveSession(name,host,port,username,password);
			}
		} else {
			showToast(getResources().getString(R.string.display_session_name_txt));
		}
	}

	private void saveSession(String name, String host, String port,
			String username, String password) {
		db.Save(new Session(name, host, port, username, password));
		AddSessionActivity.this.finish();
	}

	private Boolean isSessionNameValid(String sessionName) {
		List<Session> sessions = db.Get(Session.class);
		for(Session session : sessions) {
			if(session.sessionName.equalsIgnoreCase(sessionName)) {
				return false;
			}
		}
		return true;
	}

	private boolean isDataValid(String name,String host,String port,String username,String password) {

		if(!Validation.isString(name)) {
			showToast(getResources().getString(R.string.prompt_session_name_txt));
			return false;
		}
		if(!Validation.isHost(host)) {
			showToast(getResources().getString(R.string.prompt_host_txt));
			return false;
		}
		if(!Validation.isPort(port)) {
			showToast(getResources().getString(R.string.prompt_port_txt));
			return false;
		}
		if(!Validation.isUsername(username)) {
			showToast(getResources().getString(R.string.prompt_username_txt));
			return false;
		}
		if(!Validation.isPassword(password)) {
			showToast(getResources().getString(R.string.prompt_password_txt));
			return false;
		}
		return true;
	}


}		
